<?php

/**
 * Project: Espolon
 * Licence: BSD License
 * Link: http://espolon.org/
 * Copyright: Nicolas de Bari Embriz Garcia Rojas <nbari at espolon dot org>
 * Author: Nicolas de Bari Embriz Garcia Rojas <nbari at espolon dot org>
 * Version: Beta
 */

if (!defined('SITE_ROOT')) exit('No direct script access allowed');

class AddUser extends HTML_QuickForm {
  private $tpl;
  private $user;

  function AddUser($tpl,$user) {
    $this->tpl = $tpl;
    $this->user = $user;

    $roles = $user->getRoles();
    $this->tpl->assign('roles', $roles);

    $this->setDefaults(array('status' => 1));

    parent::HTML_QuickForm('adduser','post','','','onsubmit="javascript:password1.value=hex_sha1(password1.value), password2.value=hex_sha1(password2.value)"');
    $this->addElement('header', 'header', 'adduser');
    $this->addElement('text', 'firstname', _('firstname'), array('size' => 30, 'maxlength' => 50));
    $this->addElement('text', 'lastname', _('lastname'), array('size' => 30, 'maxlength' => 50));
    $this->addElement('text', 'email', _('email'), array('size' => 30, 'maxlength' => 100));
     $this->addElement('text', 'login', _('login'), array('size' => 15, 'maxlength' => 20));
    $this->addElement('password', 'password1', _('password1'), array('size' => 30, 'maxlength' => 50, 'value' => uniqid()));
    $this->addElement('password', 'password2', _('password2'), array('size' => 30, 'maxlength' => 50, 'value' => uniqid()));
    $sex[] =& $this->createElement('radio', null, null, _('male'), 'M', array('class' => 'radio'));
    $sex[] =& $this->createElement('radio', null, null, _('female'), 'F', array('class' => 'radio'));
    $status[] =& $this->createElement('radio', null, null, _('active'), '1', array('class' => 'radio'));
    $status[] =& $this->createElement('radio', null, null, _('inactive'), '0', array('class' => 'radio'));
    $this->addGroup($status, 'status', _('status'), '&nbsp;');
    $this->addGroup($sex, 'sex', _('sex'), '&nbsp;');
    if (!empty($roles)) {
      foreach ($roles as $value) {
        $checkbox[] =& $this->addElement('checkbox', $value['rid'], $value['name']);
      }
      $this->addGroup($checkbox, 'checkbox', null);
    }
    $this->addElement('submit', 'btn_Submit', _('submit'), array('class' => 'submit', 'onmouseover' => 'this.style.backgroundColor = \'#E0E7E7\'; this.style.borderColor = \'#728FA4\';', 'onmouseout' => 'this.style.backgroundColor = \'#eeeeee\'; this.style.borderColor = \'#999999\';'));
    $this->applyFilter('__ALL__', 'trim');
    $this->registerRule('compare','function','compare_field', $this);
    $this->registerRule('check_exists_email','function','checkExistsEmail', $this);
    $this->registerRule('check_exists_login','function','checkExistsLogin', $this);
    $this->addRule('email', _('email required'), 'required');
    $this->addRule('email', _('verify your email'), 'email');
    $this->addRule('email', _('email already exists'), 'check_exists_email', $this->user);
    $this->addRule('login', _('username required'), 'required');
    $this->addRule('login', _('min lenght 3'), 'minlength', '3');
    $this->addRule('login', _('characters not allowed'), 'regex','/^[a-zA-z0-9.-_]+$/');
    $this->addRule('login', _('username already exists'), 'check_exists_login', $this->user);
    $this->addRule('password1', _('password required'), 'required');
    $this->addRule('password2', _('password required'), 'required');
    $this->addRule('password1', _('min lenght 4'), 'minlength', '4');
    $this->addRule('password2', _('min lenght 4'), 'minlength', '4');
    $this->addRule('password1', _('passwords do not match'), 'compare', 'password2');
    $this->addRule('sex', $tpl->get_config_vars('sex required'), 'required');
  }

  function compare_field($element, $value) {
    if ($element == $this->getElementValue($value)) {
      return true;
    } else {
      return false;
    }
  }

  function checkExistsEmail() {
    $this->user->setEmail($this->getSubmitValue('email'));
    return $this->user->checkExists($op='email');
  }

  function checkExistsLogin() {
    $this->user->setLogin($this->getSubmitValue('login'));
    return $this->user->checkExists($op='login');
  }

  function displayForm() {
    $this->renderer =& new HTML_QuickForm_Renderer_ArraySmarty($this->tpl);
    $this->renderer->setRequiredTemplate(
  		    '{if $error}
  		 	     <font color="red">{$label}</font>
  	 	     {else}
  	 		     {$label}
  		 	     {if $required}
  	            <font color="red" size="1">*</font>
  	   	     {/if}
  	       {/if}'
    );
    $this->renderer->setErrorTemplate(
          '{if $error}
  	  	    <font color="blue" size="1">{$error}</font><br />
  	       {/if}{$html}'
    );
    $this->setRequiredNote(
   		'<small class="red">*&nbsp;</small><small class="blue">'._('required fields').'</small>'
   	);
   	$this->accept($this->renderer);
   	$this->tpl->assign('form_data',$this->renderer->toArray());
   	$this->tpl->assign('msg',$this->user->getMsg());
  }

  function process() {
    $this->user->addUser($this->getSubmitValues());
    $this->tpl->assign('result',true);
    $this->tpl->assign('msg',$this->user->getMsg());
  }

} // end of class

?>